
export function tree(list, pid = null) {
    let treeList = [];

    for (var i = 0; i < list.length; i++) {
        let item = list[i];
        if (item.pid == pid) {
            let childList = tree(list, item.id);
            if (childList.length) {
                item.children = childList;
            }
            treeList.push(item);
        }
    }
    return treeList;
}


export function flatTree(treeList, pid = null) {
    treeList = JSON.parse(JSON.stringify(treeList));
    let tree = [];
    for (let i = 0; i < treeList.length; i++) {
        let item = treeList[i];
        item.pid = pid;
        tree.push(item);
        if (item.children) {
            let childTree = flatTree(item.children, item.id);
            if (childTree.length) {
                tree.push(...childTree);
            }
            delete item.children;// 在children中数据拉平之后, 变相删除children 
        }
    }
    tree.sort((a, b) => a.id - b.id);
    return tree;
}